$(function(){
    //Setting up short names for used objects.
    var $cat=$("input.cat");
    var $uid=$("#uid");
    var $bar=$("input.bar")
    //Initializing Catelogy settings.
    if (localStorage.cat) 
    {cat=JSON.parse(localStorage.cat);}
    //If catelogy have not be set.
    //Load these settings as the default ones.
    else
    {cat=[1,1,0,1,0,0,0];}
    //Check the checkboxes with the loaded catelogy settings.
    for (i=0;i<=6;i++)
    {
	if (cat[i]==1)
	{$cat.eq(i).attr("checked","checked");}
    }
    //Initializing UID settings.
    if (localStorage.uid)
    {uid=localStorage.uid;
     $uid.val(uid);
    }
    //Initializing bar settings.
    if (localStorage.bar&&localStorage.bar=="0")
    {$bar.removeAttr("checked");}
    //Bind save action to save button.
    $("#btn").click(function(){
	//The catelogy checkboxes must not all be unchecked.
	if($cat.filter(":checked").length!=0)
	{
	    //Saving catelogy settings and UID settings.
	    var cat=new Array();
	    var uid=$uid.val();
	    var catname=new Array();
	    for (i=0;i<=6;i++)
	    {
		if ($cat.eq(i).attr("checked")=="checked")
		{cat[i]=1;
		 catname.push($cat.eq(i).attr("name"));
		}
		else
		{cat[i]=0;
		}
	    }
	    localStorage.cat=JSON.stringify(cat);
	    localStorage.uid=uid;
	    localStorage.catstr=catname.join(",");
	    //Saving bar settings.
	    if ($bar.attr("checked")=="checked")
	    {localStorage.bar="1";}
	    else
	    {localStorage.bar="0";}
	    alert("已保存！");
	}
	else
	{
	    alert("至少选中一个分类！");
	}
	});
});
